package com.example.zhangwei.widgts;

import java.lang.ref.WeakReference;

import com.example.zhangwei.R;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;


public class MyPopUpWindow
{
	private WeakReference<Activity> activity;
	
	private PopupWindow window;
	
	public MyPopUpWindow(Activity activity)
	{
		this.activity = new WeakReference<Activity>(activity);
		init();
	}

	private void init()
	{
		//内容
		String[] listViewValue = activity.get().getResources().getStringArray(R.array.popupwindow_listview_value);
		ListView list =new ListView(activity.get());
		list.setAdapter(new ArrayAdapter<String>(activity.get(),
				android.R.layout.simple_list_item_1, listViewValue));
		
		list.setOnItemClickListener(new mOnItemClickListener());

		//基本
		window = new PopupWindow(list);
        window.setWidth(280);
        window.setHeight(220);

        // 设置背景，否则点击外部区域没反应
        window.setBackgroundDrawable(activity.get().getResources().getDrawable(android.R.color.transparent));

        // 使窗口里面的空间显示其相应的效果，比较点击button时背景颜色改变。
        // 如果为false点击相关的空间表面上没有反应，但事件是可以监听到的。
        // listview的话就没有了作用。
        window.setFocusable(true);

        // 动画
        window.setAnimationStyle(R.style.my_popupwindow_left);
	}
	
	public void show(boolean isDown, View view)
	{
		if (!window.isShowing())
		{
			if (isDown)
			{
				window.showAsDropDown(view, 0, 0);
			}
			else
			{
				window.showAtLocation(view, Gravity.CENTER, 0,
						0);
			}					
		}
	}
	
	public boolean isShowing()
	{
		return window != null && window.isShowing();
	}
	
	public void hide()
    {
        if (window != null && window.isShowing())
        {
            window.dismiss();
        }
    }
	

	class mOnItemClickListener implements OnItemClickListener
	{

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) 
		{
			switch(arg2)
			{	        	
			case 0:
				
				break;
			case 1:

				break;
			case 2:

				break;
			default:
				break;
			}
		}
	}
}
